import { Component } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {aliasTransformFactory} from "@angular/compiler-cli/src/ngtsc/transform/src/alias";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'security ';
  authenticated =false;
  credentials = {username: 'tom',
    password:"1"
  };
  order = {};
  constructor(private http: HttpClient){
    //请求调转回来.判断是不是登录了.
    this.http.get("me").subscribe(data=>{
      if(data){
        console.log(data)
        console.log(this.authenticated)
        this.authenticated=true
      }
      if(!this.authenticated){
        console.log("去认证服务器获取授权码")
        window.location.href="http://localhost:9090/oauth/authorize?response_type=code&client_id=admin&"+
          "redirect_url=http://localhost:8080/oauth/callback&"+
          "state=url";

      }

    })


  }

  getOrder(){
    this.http.get('api/order/orders/getInfo/1').subscribe(data=>{
      console.log(data)
        let value = data;
       let result = JSON.stringify(value)
      this.order = data;
      },
      (e)=>{
      console.log(e)
        alert("get order fail")
      })
  }
  logout(){
    this.http.post("logout",this.credentials).subscribe(()=>{
      this.authenticated=false;
    },()=>{
      alert('login fail')
    })
  }
  login(){
    this.http.post('login',this.credentials).subscribe(()=>{
      this.authenticated=true;
    },()=>{
      alert("login fail")
    })
  }
}
